package com.qf.channel;

import com.alibaba.fastjson.JSON;
import com.qf.entity.Msg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@ChannelHandler.Sharable
@Order(0)
public class MsgOutChannelHandler extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof Msg) {
            Msg m = (Msg) msg;
            msg = new TextWebSocketFrame(JSON.toJSONString(m));
        }

        super.write(ctx, msg, promise);
    }
}
